Technical Q&A
ME 08 - Temporary Memory (24-Aug-98)
Q I'm writing a
background-only application
which typically has very small memory requirements. However, in some
circumstances the application needs a lot of memory for a short
period of time. I would use temporary memory, but
Inside Macintosh:
Memory says that I can't leave that memory locked around calls to
WaitNextEvent . Should I use the system heap instead?
|
A No. DTS believes that it's better for
your application to allocate and lock temporary memory than to
allocate memory in the system heap. This opinion is based on a number
of factors, described below. |
The original reference from
Inside Macintosh:
Memory (pp. 2-10) is:
[...] you must never lock temporary memory across
calls to GetNextEvent or WaitNextEvent
[...]
However, the text should read:
[...] you should avoid locking temporary memory across
calls to GetNextEvent or WaitNextEvent
[...]
If you leave a temporary memory handle locked while yielding time
to other processes, you run the risk of fragmenting the Process
Manager heap. If the Process Manager heap is fragmented, the user may
not be able to launch an application, even though there is enough
total memory to launch it. This is a very frustrating user
experience, partly because there is no way for a user to work out
which application is responsible for the block that's fragmenting the
heap. |
However, if your memory requirements are indeed temporary, it is
acceptable to allocate the memory, lock it, use it for a while, and
then unlock or deallocate it. This is certainly better than any of
the alternatives: |
- Using a bigger partition size, which just consumes the user's
resources even when you don't need them;
- Using the system heap, where allocations are not tracked (so
they aren't cleaned up if you quit unexpectedly) and are always
held (and hence reduce the efficiency of the virtual memory
system); or
- Launching another background-only application to do the job,
which complicates your code and still fragments the Process
Manager heap in the same way.
|
If you do lock a temporary memory handle, you should call
MoveHHi on it beforehand. This acts to minimize Process
Manager heap fragmentation. |
-- Quinn "The Eskimo!"
Worldwide Developer Technical Support
Technical Q&A
Previous Question |
Contents
To contact us, please use the Contact
Us page.
|